package cn.tedu.web.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;


@SuppressWarnings("all")
public class EmailTemplateUtil {

	public static String get(String name, String link) {
        // 绝对路径
        String mailTemp = "D:\\Git\\EcWeb\\ecweb-system\\src\\main\\resources\\template\\mail\\mail-tpl.xml";
        
        //获取相对路径
        Resource resource = new ClassPathResource("template/mail/mail-tpl.xml");
        String path = null;
        try {
            path = resource.getFile().getPath();
            System.out.println(path);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
		// 1. 读取 mail-tpl.xml 中的内容
        
        File file = new File(path);
		String html = "";
		try {
			List<String> strings = IOUtils.readLines(new FileInputStream(file), "UTF-8");
			html = StringUtils.join(strings, "");
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 2. 将读取的内容中的特殊变量替换为传入的名称和链接
		return html.replaceAll("#\\{name\\}", name).replaceAll("#\\{link\\}", link);
	}
}